package in.muton.panels.paginator;

import in.muton.edushare.pages.EduShareBasePage;
import in.muton.edushare.pages.publicpages.DeptWideLink;
import in.muton.edushare.pages.publicpages.UserSquareLink;
import in.muton.panels.paginator.badgehelpers.course.BatchBadgeInterface;
import in.muton.panels.paginator.badgehelpers.course.CourseBadgeInterface;
import in.muton.panels.paginator.badgehelpers.course.DeptBadgeInterface;
import in.muton.panels.paginator.badgehelpers.course.ProgramBadgeInterface;
import in.muton.panels.paginator.badgehelpers.user.BadgeRoles;
import in.muton.panels.paginator.components.badges.BatchBadge;
import in.muton.panels.paginator.components.badges.CourseBadge;
import in.muton.panels.paginator.components.badges.CourseBadgeWide;
import in.muton.panels.paginator.components.badges.CurriculaBadge;
import in.muton.panels.paginator.components.badges.DeptBadge;
import in.muton.panels.paginator.components.badges.GroupBadge;
import in.muton.panels.paginator.components.badges.UnivBadge;
import in.muton.panels.paginator.components.badges.UserBadge;
import in.muton.panels.paginator.helpers.BadgeInterface;

import java.util.List;

import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PageableListView;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;

import com.edushare.common.vo.Batches;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class EdushareListView<T> extends Panel {

	IModel<List<IModel<T>>> listModel = null;
	PageableListView<IModel<T>> pageableListView = null;
	EdusharePageNavigator pageNavigator = null;

	public EdushareListView(String id, IModel<List<IModel<T>>> listModel,
			BadgeInterface b) {
		this(id, listModel, 0);
		bif = b;
	}

	public EdushareListView(String id, IModel<List<IModel<T>>> listModel,
			int type, BadgeInterface b) {
		this(id, listModel, type);
		bif = b;
	}

	public EdushareListView(String id, IModel<List<IModel<T>>> listModel) {
		this(id, listModel, 0);
	}

	public EdushareListView(String id, IModel<List<IModel<T>>> listModel,
			int type) {
		super(id);
		this.listModel = listModel;
		this.type = type;
		pageableListView = new PageableListView<IModel<T>>("list", listModel,
				20) {
			private static final long serialVersionUID = -1132368967772133210L;

			@Override
			protected void populateItem(ListItem<IModel<T>> arg0) {
				EdushareListView.this.populateItem(arg0);
			}
		};

		pageNavigator = new EdusharePageNavigator("navigator", pageableListView);
		add(pageableListView);
		add(pageNavigator);
		pageNavigator.setOutputMarkupPlaceholderTag(false);
		pageNavigator.setRenderBodyOnly(true);
		pageableListView.setOutputMarkupPlaceholderTag(false);
		pageableListView.setRenderBodyOnly(true);
	}

	@SuppressWarnings("unchecked")
	protected void populateItem(ListItem<IModel<T>> arg0) {
		T object = arg0.getModelObject().getObject();
		if (object instanceof Departments) {
			switch (type) {
			case DEFAULT:
				arg0.add(new DeptBadge("listItem", (EduShareBasePage) this
						.getPage(),
						(IModel<Departments>) arg0.getModelObject(),
						(DeptBadgeInterface) bif));
				break;
			case WIDE:
				arg0.add(new DeptWideLink("listItem",
						(IModel<Departments>) arg0.getModelObject()));
				break;
			}
		} else if (object instanceof Courses) {
			switch (type) {
			case (DEFAULT):
				if (this.getPage() instanceof EduShareBasePage)
					arg0.add(new CourseBadge("listItem",
							(EduShareBasePage) this.getPage(),
							(IModel<Courses>) (arg0.getModelObject()),
							(CourseBadgeInterface) bif));
				else
					arg0.add(new CourseBadge("listItem", null,
							(IModel<Courses>) (arg0.getModelObject()), null));
				break;
			case (WIDE):
				arg0.add(new CourseBadgeWide("listItem",
						(IModel<Courses>) (arg0.getModelObject())));
				break;
			}
		} else if (object instanceof Users) {
			switch (type) {
			case DEFAULT:
				if (this.getPage() instanceof EduShareBasePage)
					arg0.add(new UserBadge("listItem", (EduShareBasePage) this
							.getPage(), (IModel<Users>) arg0.getModelObject(),
							(BadgeRoles) bif));
				else
					arg0.add(new UserBadge("listItem", null,
							(IModel<Users>) arg0.getModelObject(),
							(BadgeRoles) bif));
				break;

			case SQUARE:
				arg0.add(new UserSquareLink("listItem", (IModel<Users>) arg0
						.getModelObject()));
				break;
			}

		} else if (object instanceof Groups) {
			if (this.getPage() instanceof EduShareBasePage)
				arg0.add(new GroupBadge("listItem", (IModel<Groups>) arg0
						.getModelObject(), (EduShareBasePage) this.getPage()));
			else
				arg0.add(new GroupBadge("listItem", (IModel<Groups>) arg0
						.getModelObject(), null));
			
		} else if (object instanceof Universities) {
			arg0.add(new UnivBadge("listItem", (IModel<Universities>) arg0
					.getModelObject()));
		} else if (object instanceof Curricula) {
			arg0.add(new CurriculaBadge("listItem", (EduShareBasePage) this
					.getPage(), (IModel<Curricula>) arg0.getModelObject(),
					(ProgramBadgeInterface) bif));
		} else if (object instanceof Batches) {
			arg0.add(new BatchBadge("listItem", (EduShareBasePage) this
					.getPage(), (IModel<Batches>) arg0.getModelObject(),
					(BatchBadgeInterface) bif));
		} else if (object instanceof CourseInstances) {
			arg0
					.add(new CourseBadge("listItem", (EduShareBasePage) this
							.getPage(), (IModel<CourseInstances>) arg0
							.getModelObject()));
		} else {
			arg0.add(new EmptyPanel("listItem"));
		}
		arg0.setRenderBodyOnly(true);
		return;
	}

	private BadgeInterface bif = null;
	private int type = 0;
	public static final int DEFAULT = 0;
	public static final int WIDE = 1;
	public static final int SQUARE = 2;
	private static final long serialVersionUID = -7888203698480270383L;
}
